<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width: 100%"
  >
    <!-- 左侧内容 -->
    <el-col>
      <span>{{ treeNode.name }}</span>
    </el-col>
    <!-- 右侧内容 -->
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 -->
          <el-dropdown @command="operateDepts">
            <span>
              操作
              <i class="el-icon-arrow-down"></i>
            </span>
            <!-- 具名插槽 -->
            <el-dropdown-menu slot="dropdown">
              <!-- 下拉选项 -->
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">
                编辑部门
              </el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">
                删除部门
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments } from '@/api/departments'
export default {
  name: '',
  components: {},
  props: {
    treeNode: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      // required: true
      default: false
    }
  },
  data () {
    return {

    }
  },

  computed: {},
  watch: {},
  created () { },
  mounted () { },
  methods: {
    operateDepts (type) {
      if (type === 'add') {
        // 执行添加的操作
        // console.log('添加')
        this.$emit('addDepts', this.treeNode)// 为何传出treeNode 因为是添加子部门 需要当前部门
      } else if (type === 'edit') {
        // 执行编辑的操作
        this.$emit('editDepts', this.treeNode)
      } else {
        // 执行删除的操作
        this.$confirm('确定要删除该部门吗').then(() => {
          // 点击了确定进入then
          return delDepartments(this.treeNode.id)// 返回promise对象
        }).then(() => {
          // 执行成功了进入这里
          this.$emit('delDepts')
          this.$message('删除成功')
        })
      }
    }
  }
}
</script>
<style lang='css' scoped>
</style>
